<template>
  <el-card>
    <div slot="header">
          <el-button
            :disabled="loading"
            @click="$router.push({ name: 'advert-create' })"
          >添加广告</el-button>
    </div>
    <el-table
      :data="advert"
      style="width: 100%"
      height="500"
      size="medium"
      v-loading="loading"
    >
      <el-table-column
        prop="id"
        label="ID"
        width="100">
      </el-table-column>
      <el-table-column
        prop="name"
        label="广告名称"
        width="120">
      </el-table-column>
      <el-table-column
        prop="spaceId"
        label="广告位置"
        width="120">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="时间"
        width="120">
        <template slot-scope="scope">
          <div>开始时间: {{scope.row.startTime}}</div>
          <div>到期时间: {{scope.row.endTime}}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="广告图片"
        >
        <template slot-scope="scope">
          <img width="80px" :src="scope.row.img || 'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png'">
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="handleForbidUser(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleSelectRole(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="分配角色"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <el-select v-model="roleIdList" multiple placeholder="请选择">
        <el-option
          v-for="item in roles"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handleAllocRole"
        >确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
import Vue from 'vue'
import { forbidUser } from '@/services/user'

import { getQueryAdvert, createOrUpdateAdvert, getEditAdvertInfo, changeAdvertState } from '@/services/advert'

import { Form } from 'element-ui'
import {
  getAllRoles,
  allocateUserRoles,
  getUserRoles
} from '@/services/role'

export default Vue.extend({
  name: 'AdvertList',
  data () {
    return {
      advert: [],
      filterParams: {
        currentPage: 1,
        pageSize: 10,
        phone: '',
        startCreateTime: '',
        endCreateTime: '',
        rangeDate: []
      },
      totalCount: 0,
      loading: true,
      dialogVisible: false,
      roles: [],
      roleIdList: [],
      currentUser: null // 分配角色的当前用户
    }
  },

  created () {
    this.loadUsers()
  },

  methods: {
    async loadUsers () {
      this.loading = true
      const { data } = await getQueryAdvert()
      this.advert = data.content
      this.loading = false
    },
    async handleForbidUser (item: any) {
      const { data } = await changeAdvertState(item.id)
      if (data.success) {
        this.$message.success('操作成功')
      } else {
        item.status = item.status === 1 ? 0 : 1
        this.$message.error('操作失败')
      }
      console.log(data)
    },

    handleReset () {
      (this.$refs['filter-form'] as Form).resetFields()
      this.loadUsers()
    },

    async handleSelectRole (item: any) {
      this.$router.push({
        name: 'advert-edit',
        params: {
          id: item.id
        }
      })
    },

    async handleAllocRole () {
      await allocateUserRoles({
        userId: (this.currentUser as any).id,
        roleIdList: this.roleIdList
      })
      this.$message.success('操作成功')
      this.dialogVisible = false
    }
  }
})
</script>

<style lang="scss" scoped></style>
